﻿<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:o="urn:schemas-microsoft-com:office:office"
  xmlns:x="urn:schemas-microsoft-com:office:excel"
  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
  <xsl:template match="/">
    <xsl:processing-instruction name="mso-application">
      <xsl:text>progid="Excel.Sheet"</xsl:text>
    </xsl:processing-instruction>
    <ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
      <Styles>
        <Style ss:ID="s62">
          <NumberFormat ss:Format="Short Date"/>
          <Borders>
            <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
            <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
            <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
            <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
          </Borders>
        </Style>
        <Style ss:ID="s63">
          <Borders>
            <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
            <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
            <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
            <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
          </Borders>
        </Style>
      </Styles>      
      <ss:Worksheet ss:Name="Книга 1">
        <ss:Table>
          <xsl:for-each select="//Columns/Column">
            <Column ss:AutoFitWidth="0">
              <xsl:attribute name="ss:Width">
                <xsl:value-of select="Width"/>
              </xsl:attribute>
            </Column>
          </xsl:for-each>
            <ss:Row>
            <xsl:for-each select="//Columns/Column">
              <ss:Cell ss:StyleID="s63">
                <ss:Data ss:Type="String">
                  <xsl:value-of select="Header"/>
                </ss:Data>
              </ss:Cell>
            </xsl:for-each>
          </ss:Row>
          <xsl:for-each select="//Data/Record">
            <ss:Row>
              <xsl:for-each select=".">
                <!--Запоминаем текущую позицию в списке record для дальнейщего нахождения значения по названию -->
                <xsl:variable name="record" select="."/>
                <xsl:for-each select="//Columns/Column">
                  <!-- Сохраняем название поля из настройки -->
                  <xsl:variable name="name" select="Name"/>
                  <xsl:variable name="type" select="Type"/>
                  <!-- Эта переменная возвращает значение записи по имени поля взятой из списка настроек -->
                  <xsl:variable name="value-of-field">
                    <xsl:for-each select="$record/*">
                      <xsl:if test="name(.) = $name">
                        <xsl:value-of select="."/>
                      </xsl:if>
                    </xsl:for-each>
                  </xsl:variable>
                  <!-- Рисует ячейку с передаваемыми -->
                  <xsl:choose>
                    <xsl:when test="$value-of-field=''">
                      <ss:Cell ss:StyleID="s63"></ss:Cell>
                    </xsl:when>
                    <xsl:when test="$type='number'">
                      <ss:Cell ss:StyleID="s63">
                        <ss:Data ss:Type="Number" >
                          <xsl:value-of select="$value-of-field"/>
                        </ss:Data>
                      </ss:Cell>
                    </xsl:when>
                    <xsl:when test="$type='date'">
                      <ss:Cell ss:StyleID="s62">
                        <ss:Data ss:Type="DateTime">
                          <xsl:value-of select="$value-of-field"/>
                        </ss:Data>
                      </ss:Cell>
                    </xsl:when>
                    <xsl:when test="($type='int')">
                      <ss:Cell ss:StyleID="s63">
                        <ss:Data ss:Type="Number">
                          <xsl:value-of select="$value-of-field"/>
                        </ss:Data>
                      </ss:Cell>
                    </xsl:when>
                    <xsl:otherwise>
                      <ss:Cell ss:StyleID="s63">
                        <ss:Data ss:Type="String">
                          <xsl:value-of select="$value-of-field"/>
                        </ss:Data>
                      </ss:Cell>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:for-each>
              </xsl:for-each>
            </ss:Row>
          </xsl:for-each>

        </ss:Table>
      </ss:Worksheet>
    </ss:Workbook>
  </xsl:template>
</xsl:stylesheet>
